get_load_new <- function(path =  "path_name/",        
                         file_name) {
  
  data_name <- load(file=paste0(path,file_name))
  return(get(data_name))
}

paste_new_preds_to_original_data <- function(data = test_dt_withProb_cancer ,
                                             file_name = "data_original_new_pipeline.RData" ){
  
  temp <- merge(x = data, 
                y = data.table(get_load_new( file_name = file_name  )$preds_test)[,.(id_var , new_preds = preds_after_bayes)],
                by = "id_var"  )
  
  temp[,prob_for_report :=new_preds ]
  return(temp)
}



calibration_plot <- function(data,preds,true,bins=20 ) {
  data <- data.table(data)
  
  data[ ,  .(actual_rate   = mean(get(true) == "1"),
             pred_mortality = mean(get(preds))
  ),
  by = .(bins = gtools::quantcut(get(preds),
                                 seq(0,1,1/bins),
                                 right = TRUE,
                                 labels = as.character(seq(0.01,1,1/bins))))
  ] %>%
    ggplot(data = .) +
    geom_point(aes(x = pred_mortality, 
                   y = actual_rate),
               size = 3) +
    geom_abline(intercept = 0, slope = 1, linetype = 3) +
    scale_x_continuous(limits = c(0,1),breaks=seq(0, 1, 0.2)) +
    scale_y_continuous(limits = c(0,1),breaks=seq(0, 1, 0.2)) +
    labs(x = "Average Predicted Mortality Risk",
         y = "Actual Mortality Rate",
         caption = paste0(bins," bins") )+ 
    theme(aspect.ratio = 1)
} 
